// 团队相关类型定义

export interface Team {
  id: number
  name: string
  description?: string
  status: 'PENDING' | 'APPROVED' | 'REJECTED' | 'ARCHIVED'
  creatorId: number
  creatorName?: string  // 创建者用户名
  currentMembers: number
  maxMembers: number
  teamAvatarUrl?: string
  createdAt: string
  updatedAt: string
}

export interface TeamMember {
  id: number
  teamId: number
  userId: number
  username?: string
  nickname?: string
  avatarUrl?: string
  role: 'OWNER' | 'ADMIN' | 'MEMBER'
  status: 'ACTIVE' | 'INACTIVE' | 'REMOVED'
  joinedAt: string
  isOnline?: boolean
  lastActiveTime?: string
  onlineStatus?: string
}

export interface TeamInviteCode {
  id: number
  teamId: number
  inviteCode: string
  creatorId: number
  creatorUsername?: string
  inviteRole: 'ADMIN' | 'MEMBER'
  maxUses?: number
  usedCount: number
  expireTime?: string
  status: 'ACTIVE' | 'EXPIRED' | 'DISABLED' | 'EXHAUSTED'
  createdAt: string
  updatedAt: string
  isValid: boolean
  remainingUses?: number
}

// 请求类型
export interface CreateTeamRequest {
  name: string
  description?: string
  maxMembers?: number
  teamAvatarUrl?: string
}

export interface UpdateTeamRequest {
  name: string
  description?: string
  maxMembers?: number
  teamAvatarUrl?: string
}

export interface AddMemberRequest {
  userId: number
  role?: TeamMember['role']
}

export interface UpdateMemberRoleRequest {
  role: TeamMember['role']
}

export interface GenerateInviteCodeRequest {
  inviteRole?: TeamInviteCode['inviteRole']
  expireTime?: string
  maxUses?: number
}

// 团队状态选项
export const TEAM_STATUS_OPTIONS = [
  { label: '待审核', value: 'PENDING', type: 'warning' },
  { label: '已通过', value: 'APPROVED', type: 'success' },
  { label: '已拒绝', value: 'REJECTED', type: 'danger' },
  { label: '已归档', value: 'ARCHIVED', type: 'info' }
] as const

// 成员角色选项
export const MEMBER_ROLE_OPTIONS = [
  { label: '所有者', value: 'OWNER', type: 'danger' },
  { label: '管理员', value: 'ADMIN', type: 'warning' },
  { label: '成员', value: 'MEMBER', type: 'primary' }
] as const

// 成员状态选项
export const MEMBER_STATUS_OPTIONS = [
  { label: '活跃', value: 'ACTIVE', type: 'success' },
  { label: '非活跃', value: 'INACTIVE', type: 'warning' },
  { label: '已移除', value: 'REMOVED', type: 'danger' }
] as const

// 邀请码状态选项
export const INVITE_CODE_STATUS_OPTIONS = [
  { label: '活跃', value: 'ACTIVE', type: 'success' },
  { label: '已过期', value: 'EXPIRED', type: 'warning' },
  { label: '已禁用', value: 'DISABLED', type: 'danger' },
  { label: '已用完', value: 'EXHAUSTED', type: 'info' }
] as const

// 工具函数
export const getTeamStatusLabel = (status: Team['status']) => {
  return TEAM_STATUS_OPTIONS.find(option => option.value === status)?.label || status
}

export const getTeamStatusType = (status: Team['status']) => {
  return TEAM_STATUS_OPTIONS.find(option => option.value === status)?.type || 'primary'
}

export const getMemberRoleLabel = (role: TeamMember['role']) => {
  return MEMBER_ROLE_OPTIONS.find(option => option.value === role)?.label || role
}

export const getMemberRoleType = (role: TeamMember['role']) => {
  return MEMBER_ROLE_OPTIONS.find(option => option.value === role)?.type || 'primary'
}

export const getMemberStatusLabel = (status: TeamMember['status']) => {
  return MEMBER_STATUS_OPTIONS.find(option => option.value === status)?.label || status
}

export const getMemberStatusType = (status: TeamMember['status']) => {
  return MEMBER_STATUS_OPTIONS.find(option => option.value === status)?.type || 'primary'
}

export const getInviteCodeStatusLabel = (status: TeamInviteCode['status']) => {
  return INVITE_CODE_STATUS_OPTIONS.find(option => option.value === status)?.label || status
}

export const getInviteCodeStatusType = (status: TeamInviteCode['status']) => {
  return INVITE_CODE_STATUS_OPTIONS.find(option => option.value === status)?.type || 'primary'
}

// 权限检查函数
export const canManageTeam = (userRole: TeamMember['role']) => {
  return ['OWNER', 'ADMIN'].includes(userRole)
}

export const canDeleteTeam = (userRole: TeamMember['role']) => {
  return userRole === 'OWNER'
}

export const canInviteMembers = (userRole: TeamMember['role']) => {
  return ['OWNER', 'ADMIN'].includes(userRole)
}

export const canRemoveMembers = (userRole: TeamMember['role']) => {
  return ['OWNER', 'ADMIN'].includes(userRole)
}

export const canUpdateMemberRole = (userRole: TeamMember['role'], targetRole: TeamMember['role']) => {
  if (userRole === 'OWNER') return true
  if (userRole === 'ADMIN' && targetRole !== 'OWNER') return true
  return false
}

// 团队状态类型
export type TeamStatus = 'PENDING' | 'APPROVED' | 'REJECTED' | 'ARCHIVED'

// 团队可见性类型
export type TeamVisibility = 'PUBLIC' | 'INTERNAL' | 'PRIVATE'

// 团队加入方式类型
export type TeamJoinMethod = 'INVITE_ONLY' | 'REQUEST' | 'OPEN'

// 团队设置接口
export interface TeamSettings {
  teamId: number
  visibility: TeamVisibility
  joinMethod: TeamJoinMethod
  defaultPermissions: string[]
  enableNotifications: boolean
  notificationTypes: string[]
  requireTwoFactor: boolean
  sessionTimeout: number
  allowMemberInvite: boolean
  allowMemberCreateProject: boolean
  allowMemberManageFiles: boolean
}

// 团队设置请求接口
export interface TeamSettingsRequest {
  visibility: TeamVisibility
  joinMethod: TeamJoinMethod
  defaultPermissions?: string[]
  enableNotifications?: boolean
  notificationTypes?: string[]
  requireTwoFactor?: boolean
  sessionTimeout?: number
  allowMemberInvite?: boolean
  allowMemberCreateProject?: boolean
  allowMemberManageFiles?: boolean
}

// 团队设置选项
export const TEAM_VISIBILITY_OPTIONS = [
  { value: 'PUBLIC', label: '公开', description: '所有用户都可以看到这个团队' },
  { value: 'INTERNAL', label: '内部', description: '只有登录用户可以看到这个团队' },
  { value: 'PRIVATE', label: '私有', description: '只有团队成员可以看到这个团队' }
] as const

export const TEAM_JOIN_METHOD_OPTIONS = [
  { value: 'INVITE_ONLY', label: '仅邀请', description: '只能通过邀请加入团队' },
  { value: 'REQUEST', label: '申请加入', description: '用户可以申请加入，需要管理员审核' },
  { value: 'OPEN', label: '开放加入', description: '用户可以直接加入团队' }
] as const

export const DEFAULT_PERMISSION_OPTIONS = [
  { value: 'VIEW_MEMBERS', label: '查看成员列表' },
  { value: 'INVITE_MEMBERS', label: '邀请新成员' },
  { value: 'CREATE_PROJECT', label: '创建项目' },
  { value: 'MANAGE_FILES', label: '管理文件' },
  { value: 'VIEW_STATISTICS', label: '查看统计信息' }
] as const

export const NOTIFICATION_TYPE_OPTIONS = [
  { value: 'MEMBER_JOIN', label: '成员加入' },
  { value: 'MEMBER_LEAVE', label: '成员离开' },
  { value: 'ROLE_CHANGE', label: '角色变更' },
  { value: 'PROJECT_UPDATE', label: '项目更新' },
  { value: 'FILE_SHARE', label: '文件分享' }
] as const
